package com.seguriapp.entities;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.seguriapp.base.BaseEntity;

/**
 * Represents a SeguriAppEntity entity.
 * 
 * @author Tania Gonzales(taniadgv@gmail.com)
 */
@Entity
@Table(name = "Entity")
public class SeguriAppEntity extends BaseEntity implements Comparable<SeguriAppEntity> {

  private static final long serialVersionUID = 6338534531643781599L;

  @Id
  @GeneratedValue
  private Long id;

  private double latitude;
  private double longitude;
  private double altitude;
  private String title;
  private boolean underline;
  private String url;
  private boolean active;
  protected boolean isVisible;
  private String phone;
  private String address;

  @Transient
  private double distance;

  @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
  @JoinColumn(nullable = false, name = "entityTypeId")
  private SeguriAppEntityType type;

  @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
  @JoinColumn(nullable = false, name = "districtId")
  private District district;

  public Long getId() {
    return id;
  }

  public double getLatitude() {
    return latitude;
  }

  public double getLongitude() {
    return longitude;
  }

  public double getAltitude() {
    return altitude;
  }

  public String getTitle() {
    return title;
  }

  public boolean isUnderline() {
    return underline;
  }

  public String getUrl() {
    return url;
  }

  public boolean isActive() {
    return active;
  }

  public boolean isVisible() {
    return isVisible;
  }

  public String getPhone() {
    return phone;
  }

  public SeguriAppEntityType getType() {
    return type;
  }

  public double getDistance() {
    return distance;
  }

  public void setId(final Long id) {
    this.id = id;
  }

  public void setLatitude(final double latitude) {
    this.latitude = latitude;
  }

  public void setLongitude(final double longitude) {
    this.longitude = longitude;
  }

  public void setAltitude(final double altitude) {
    this.altitude = altitude;
  }

  public void setTitle(final String title) {
    this.title = title;
  }

  public void setUnderline(final boolean underline) {
    this.underline = underline;
  }

  public void setUrl(final String url) {
    this.url = url;
  }

  public void setActive(final boolean active) {
    this.active = active;
  }

  public void setVisible(final boolean isVisible) {
    this.isVisible = isVisible;
  }

  public void setPhone(final String phone) {
    this.phone = phone;
  }

  public void setType(final SeguriAppEntityType type) {
    this.type = type;
  }

  public void setDistance(final double distance) {
    this.distance = distance;
  }

  public District getDistrict() {
    return district;
  }

  public void setDistrict(final District district) {
    this.district = district;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(final String address) {
    this.address = address;
  }

  public Map<String, Object> toJsonString() {
    final Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("id", id);
    map.put("latitude", latitude);
    map.put("longitude", longitude);
    map.put("altitude", altitude);
    map.put("title", title);
    map.put("underline", underline);
    map.put("url", url);
    map.put("active", active);
    map.put("isVisible", isVisible);
    map.put("phone", phone);
    map.put("type", type);
    map.put("distance", distance);
    map.put("address", address);
    if (district != null) {
      map.put("district", district.toJsonString());
    }
    return map;
  }

  public int compareTo(final SeguriAppEntity entity) {
    if (entity.getDistance() == distance) {
      return 0;
    }
    return distance > entity.getDistance() ? 1 : -1;
  }

}
